<a onclick="copyTxt('这是要复制的内容')">点击复制</a>

  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="//resources/demos/style.css">
  <style>
  </style>
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script src="https://shc7432.github.io/public/getLinkInput/getLinkInput.js"></script>

<script>/*Core is in down*/</script>

<div class="copytextsucce" title="复制成功">
复制成功!
</div>
<div class="copytextfail" title="复制失败">
<div style="border:1px solid #ccc;background-color:#aaa;">
请按 Ctrl + C 复制<br>
<textarea class="copyfailoutput" cols="40" rows="6" style="background-color:#fff;border:0px solid #ccc;"></textarea></div>
</div>
<script>
//原生js实现复制内容到剪切板，兼容pc、移动端（支持Safari浏览器）
$(function(){

$(".copytextsucce").dialog({
autoOpen: false,
modal: true,
width: "auto",
height: "auto",
buttons: {
"确定": function(){
$(this).dialog("close");
}
 },
close: function(){
}
  })
$(".copytextfail").dialog({
autoOpen: false,
modal: true,
buttons: {
"确定": function(){
$(this).dialog("close");
}
 },
close: function(){
}
  })

} )
function copytext(text){
if(typeof document.execCommand!=="function"){
$(function(){
$(".copytextfail").dialog("open");
document.querySelectorAll(".copyfailoutput")[0].value=text;
})
return false;
}
var dom = document.createElement("textarea");
dom.value = text;
dom.setAttribute('style', 'display: block;width: 1px;height: 1px;');
document.body.appendChild(dom);
dom.select();
var result = document.execCommand('copy');
document.body.removeChild(dom);
if (result) {
$(function(){
$(".copytextsucce").dialog("open");
})
return true;
}
if(typeof document.createRange!=="function"){
$(function(){
$(".copytextfail").dialog("open");
document.querySelectorAll(".copyfailoutput")[0].value=text;
})
return false;
}
var range = document.createRange();
var div=document.createElement('div');
div.innerHTML=text;
div.setAttribute('style', 'height: 1px;fontSize: 1px;overflow: hidden;');
document.body.appendChild(div);
range.selectNode(div);
const selection = window.getSelection();
if (selection.rangeCount > 0){
selection.removeAllRanges();
}
selection.addRange(range);
document.execCommand('copy');
$(function(){
$(".copytextsucce").dialog("open");
})
return true;
}
</script>
